﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BostonAzureSiteWebRole.ViewHelpers
{
    public static class SemanticMarkupHelpers
    {
        /// <summary>
        /// Generate semantic microformat HTML for a date
        /// </summary>
        /// <param name="helper"></param>
        /// <returns>formatted string (<div title="utc">Tue Jan 12, 2010 @ 7:00 PM</div>), or "" if input is null</returns>
        public static string LocalDateTimeString(this HtmlHelper helper, DateTime? dateTimeUtc)
        {
            if (dateTimeUtc.HasValue)
            {
                // in our case, we want Boston time
                // How to convert from UTC => Eastern?
                //string localIso8601BostonTime =
                //    string.Format("{0:o}", dateTimeUtc.Value.To);
                //STRING localDateTimeString = string.Format("{0:g}", dateTimeUtc.Value.ToLocalTime());
                //                DateTime localTime = ConvertUtcToBostonAzureTime(dateTimeUtc.Value);
                DateTime localTime = dateTimeUtc.Value;
                //string utcISO8601DateTimeString = string.Format("{0:o}", dateTimeUtc.Value.ToLocalTime());
                string localTimeISO8601DateTimeString = string.Format("{0:o}", localTime.ToLocalTime());
                // does not work!                string localTimeEnglishDateTimeString = localTime.ToString("0:f"); // does not work
                string localTimeEnglishDateTimeString = String.Format("{0:f}", localTime);
                //string localTimeEnglishDateTimeString = localTime.ToString("ddd MMMM d YYYY hh:mm tt");
                //                    string.Format("{0} at {1}", localTime.ToLongTimeString());
                //
                // <p>The weekly dinner will be on 
                //    <span class="dtstart">
                //        <abbr class="value" title="2008-06-24">this Tuesday</abbr> 
                //     at <span class="value">18:30</span>
                //    </span>
                //</p>
                string microformat = String.Format(
                    "<span class=\"dtstart\">" +
                       "<abbr class=\"value\" title=\"{0}\">" +
                       "{1}" +
                    "</span>",
                    localTimeISO8601DateTimeString,
                    localTimeEnglishDateTimeString);
                return microformat;
            }
            else
            {
                return "";
            }
        }
    }
}
